// pages/article-more/more.js
var model = require('../composition/model/article.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    _currentLevel: {},
    articleList: [],
    compositionTypes: [],
    pageNum: 1,
    currentTypeId: -1,
    boutique: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    var _grade = options.gradeObject
    var _boutique = options.boutique
    if (Number(_boutique)==0){
      _boutique=-1
    }
    var _type = options.type
    this.setData({
      _currentLevel: JSON.parse(_grade),
      boutique: _boutique,
      currentTypeId: Number(_type)
    })
    var that=this
    model.loadCompositionTypes(function (list) {
      var tmpList = [];
      tmpList.push({ id: -1, text: "全部", select: true })
      list.forEach(function (item, index) {
        if (item.id != -1 || item.text !== '未知类型') {
          item['select'] = false
          tmpList.push(item)
        }
      })
      tmpList.forEach(function(item,index){
        if (item.id === that.data.currentTypeId){
          item['select'] = true
        }else{
          item['select'] = false
        }
      })
      that.setData({
        compositionTypes: tmpList
      })
    })
    this.loadList(this.data._currentLevel.id, this.data.currentTypeId, null, null, null, this.data.pageNum)
  },
  loadList: function(grade, type, theme, title, tag, pageNum) {
    var that = this
    model.getMoreList(grade, this.data.boutique, type, theme, title, tag, 1, 1, pageNum, 10, function(res) {
      that.setData({
        articleList: res,
        pageNum: pageNum + 1
      })
    })
  },
  selectType: function (event) {
    var id = event.currentTarget.dataset.value;
    var types = this.data.compositionTypes;
    types.forEach(function (item, index) {
      if (item.id == id) {
        item.select = true
      } else {
        item.select = false
      }
    })
    if (id != this.data.currentTypeId) {
      this.loadList(this.data._currentLevel.id, id, null, null, null, 1)
    }
    this.setData({
      currentTypeId: id,
      compositionTypes: types,
      pageNum:1
    })
    
  },
  seeDetail: function (event) {
    var position = event.currentTarget.dataset.position
    var obj = this.data.articleList[position];
    obj['des'] = '';
    if (obj != null) {
      wx.navigateTo({
        url: '../article-detail/detail?baseInfo=' + JSON.stringify(obj),
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})